package docsync;

import java.io.File;

public class TestStubFolder implements FolderInterface {
	private	String _path;
	public TestStubFolder( String path ){
		_path = path;
	}
	
	@Override
	public String toString(){
		return _path;
	}
	
	@Override
	public FileInterface create(String filename) throws Exception {
		File f = new File(_path+File.separator+filename);
		if( ! f.createNewFile() ) throw new Exception("Fail to create new file");
		return new TestStubFile( f );
	}

	@Override
	public FileInterface[] getFiles() {
		File f = new File( _path );
		File [] files = f.listFiles();
		FileInterface [] out = new FileInterface[files.length];
		for( int i=0; i<files.length; ++i){
			out[i] = new TestStubFile(files[i]);
		}
		return out;
	}

	@Override
	public FileInterface open(String filename) {
		File f = new File( _path+File.separator+filename );
		return new TestStubFile( f );
	}

	@Override
	public void remove(String filename) throws Exception {
		File f = new File( _path+File.separator+filename );
		if( ! f.delete() ) throw new Exception("Fail to remove file");
	}
	
}
